package com.wc5818.common;
 
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.wc5818.config.ApplicationConfig;

import java.io.*;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

/**
 * 文件上传
 */


@Controller
public class FileController {
	 

 
	/***
	 * 文件上传
	 * @param file
	 * @param model
	 * @param request
	 * @return
	 */
    public String fileUpload(MultipartFile file, Model model, HttpServletRequest request,String savepath) {
      
    	 
        String fileName = file.getOriginalFilename();  // 文件名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));  // 后缀名
        
        
        String[] suffix = { ".png", ".jpg", ".jpeg", ".bmp"};
        Boolean isImage =false;
        for(String f : suffix){
        	
        	if(f.indexOf(suffixName) != -1) {
        		
        		isImage = true;
        	}
        	
        }
       
        if(!isImage) { return "0";}
        
        String filePath = savepath; // 上传后的路径
        fileName = BaseTools.getCurrentTimeMillis() + suffixName; // 新文件名
        File dest = new File(filePath + fileName);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            file.transferTo(dest);
        } catch (IOException e) {
            e.printStackTrace();
        }
     
        return fileName;
    }
}

